package com.jsonal.service.impl;

import static com.jsonal.common.FileUrl.bigImageFolderName;
import static com.jsonal.common.FileUrl.middleImageFolderName;
import static com.jsonal.common.FileUrl.originalImageFolderName;
import static com.jsonal.common.FileUrl.smallImageFolderName;

import com.jsonal.common.CommonUtil;
import com.jsonal.common.OSSBootUtil;
import com.jsonal.config.OSSConfig;
import com.jsonal.entity.SysFile;
import com.jsonal.entity.SysFileDt;
import com.jsonal.mapper.SysFileDtMapper;
import com.jsonal.service.UploadFilesService;
import com.jsonal.vo.Result;
import java.text.DecimalFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: jingji.lin
 * @Description:
 * @Date: 2020/5/4 9:06
 * @Version: 1.0
 */
@Service
@Slf4j
public class UploadFilesServiceImpl implements UploadFilesService {

    @Autowired
    OSSConfig ossConfig;

    @Autowired
    private SysFileDtMapper sysFileDtMapper;

    /**
     * 文件上传至 oss 服务器
     * @param file 文件
     * @param folder 文件夹
     * @param fileId 文件名
     * @return res
     */
    @Override
    public Result saveFiles(MultipartFile file, String fileId, String folder) {
        Result result = new Result();
        String fileSize = null;
        try {
            // 保存文件大小计量单位是b
            fileSize = (new DecimalFormat("0.00")).format(file.getSize());
        } catch (Exception ignored) {
        }

        SysFileDt fileDt = new SysFileDt();
        SysFile sysFile = new SysFile();
        sysFile.setFileId(fileId);
        fileDt.setFileId(fileId);
        fileDt.setFileDtId(CommonUtil.getGUID());
        // 文件名
        String fileName = file.getOriginalFilename();
        // 扩展名
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
        // 存在服务器上的真实的文件名+后缀，使用UUId生成
        String fileTrueName = CommonUtil.getGUID() + "." + extension;

        fileDt.setFileCode(fileTrueName);
        fileDt.setFileName(fileName);
        fileDt.setFileSize(fileSize);
        fileDt.setFileExtension(extension);
        fileDt.setFileFolder(folder);
        fileDt.setFileOriginal(originalImageFolderName.getStr());
        fileDt.setFileBig(bigImageFolderName.getStr());
        fileDt.setFileMiddle(middleImageFolderName.getStr());
        fileDt.setFileSmall(smallImageFolderName.getStr());
        fileDt.setDelFlag(false);
        // 上传文件
        try {
            //文件储存的路径
            String fileUrl = folder + "/" + originalImageFolderName.getStr() + "/" + fileTrueName;
            log.info("fileUrl >>>>>>>>>>> {}", fileUrl);
            //上传文件  ossconfig  file  存储的路径
            //return 图片存储的网络路径
            String fileurl = OSSBootUtil.upload(ossConfig, file, fileUrl);
            if (!fileurl.isEmpty()) {
                try {
                    fileDt.setFileUrl(fileurl);
                    int insert = sysFileDtMapper.insert(fileDt);
                } catch (Exception e) {
                    e.printStackTrace();
                    log.info("文件保存失败，请重试！");
                }
            }
            result.setSuccess(true);
            //存储的完整路径
            fileDt.setFileUrl(fileurl);
            fileDt.setDelFlag(false);
            fileDt.setFileHttpDir("");
            // 设置需要返回到前端显示用的参数
            result.setObj(fileDt);
            result.setMsg("上传成功");
        } catch (Exception e) {
            return getResult(result, e);
        }
        return result;
    }

    private Result getResult(Result result, Exception e) {
        e.printStackTrace();
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        result.setSuccess(false);
        result.setMsg("上传失败，原因如下：" + e.getMessage());

        return result;
    }
}
